package com.util.configuration;

import java.util.Properties;
import java.util.Map;
import java.util.HashMap;
import java.io.InputStream;

public final class ConfigurationParametersManager {

    private static final String CONFIGURATION_FILE =
        "ConfigurationParameters.properties";    

    private static Map parameters;
    
    static {

        try {
        
            /* Read property file (if exists).*/    
            Class configurationParametersManagerClass = 
                ConfigurationParametersManager.class;
            ClassLoader classLoader =
                configurationParametersManagerClass.getClassLoader();
            InputStream inputStream =
                classLoader.getResourceAsStream(CONFIGURATION_FILE);
            Properties properties = new Properties();
            properties.load(inputStream);
            inputStream.close();
            
            System.out.println("*** Using '" + CONFIGURATION_FILE +
                "' file for configuration ***");

            parameters = new HashMap(properties);
            
        } catch (Exception e) { 
            System.out.println("*** Not found configuration file: '" + CONFIGURATION_FILE + "'");
        }
        
    }
       
    private ConfigurationParametersManager() {}
    
    public static String getParameter(String name) 
        throws MissingConfigurationParameterException {
    
        String value = (String) parameters.get(name);

        if (value == null) {
            throw new MissingConfigurationParameterException(name);
        }

        return value;
                                
    }
    
    public static void setParameter(String pname, String pvalue) {
    	parameters.put(pname, pvalue);
    }   

    /* Test code. Uncomment for testing. */
//    public static void main (String[] args) {
//        System.out.println(parameters);
//    }
     
}
